#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QString picPath = QDir::currentPath() + "/" + "pic" + "/" + "1.jpg";
    m_pixmap = QPixmap(picPath);
    picW = m_pixmap.width();
    picH = m_pixmap.height();

    mAni_rotate = new QPropertyAnimation;
    mAni_scale = new QPropertyAnimation;
    mAni_opacity = new QPropertyAnimation;
    mAni_labelPos = new QPropertyAnimation;
    mAni_imgCopy = new QPropertyAnimation;
    iniAnimationOp(mAni_rotate,this,"rval",10000,1.0,360.0);
    iniAnimationOp(mAni_scale,this,"sval",10000,1,picH);
    iniAnimationOp(mAni_imgCopy,this,"p_hval",3000,0,1);

    mlGraEff = new QGraphicsOpacityEffect ;
    mlGraEff->setEnabled(true);
    iniAnimationOp(mAni_opacity,mlGraEff,"opacity",5000,0,1);

    msclpt = new QPoint;

    ml1 = new QLabel(this);
    ml1->setGeometry(wdWidth/2-picW/2,wdHeight/2-picH/2,picW,picH);

    int mlx = ml1->geometry().x();
    int mly = ml1->geometry().y();

    ml1->setPixmap(m_pixmap);
    ml1->setAlignment(Qt::AlignCenter);

    iniAnimationOp(mAni_labelPos,ml1,"pos",10000,QPoint(mlx,mly),QPoint(mlx+200,mly));

    QPushButton *mb1 = new QPushButton(this);
    mb1->setGeometry(10,450,20,20);
    mb1->setText("R");
    connect(mb1,&QPushButton::clicked,this,&MainWindow::btnR);

    QPushButton *mb2 = new QPushButton(this);
    mb2->setGeometry(50,450,20,20);
    mb2->setText("S");
    connect(mb2,&QPushButton::clicked,this,&MainWindow::btnS);

    QPushButton *mb3 = new QPushButton(this);
    mb3->setGeometry(90,450,20,20);
    mb3->setText("O");
    connect(mb3,&QPushButton::clicked,this,&MainWindow::btnO);

    QPushButton *mb4 = new QPushButton(this);
    mb4->setGeometry(130,450,40,20);
    mb4->setText("LM");
    connect(mb4,&QPushButton::clicked,this,&MainWindow::btnLM);

    QPushButton *mb5 = new QPushButton(this);
    mb5->setGeometry(190,450,60,20);
    mb5->setText("LGM");
    connect(mb5,&QPushButton::clicked,this,&MainWindow::btnLGM);
}

MainWindow::~MainWindow()
{
}

double MainWindow::getRval()
{
    return rval;
}

void MainWindow::rvalSetted()
{
    QTransform f_trans;
    f_trans.rotate(rval);
    nPixmap = new QPixmap(m_pixmap.transformed(f_trans));
    ml1->setPixmap(*nPixmap);
}


void MainWindow::setRval(int v1)
{
    rval = v1;
    rvalSetted();
}

double MainWindow::getOval()
{
    return oval;
}

void MainWindow::ovalSetted()
{
    nPixmap = new QPixmap(m_pixmap.scaled(sval,sval));
    ml1->setPixmap(*nPixmap);
}


void MainWindow::setOval(int v1)
{
    oval = v1;
    ovalSetted();
}

double MainWindow::getSval()
{
    return sval;
}

void MainWindow::svalSetted()
{
    nPixmap = new QPixmap(m_pixmap.scaled(sval,sval));
    ml1->setPixmap(*nPixmap);
}


void MainWindow::setSval(int v1)
{
    sval = v1;
    svalSetted();
}




double MainWindow::getP_hval()
{
    return p_hval;
}

void MainWindow::p_hvalSetted()
{
    QImage *fimg = new QImage(m_pixmap.toImage());
    fimg = new QImage(fimg->copy(QRect(0,0,picW,picH*p_hval)));

    QPixmap *fpm = new QPixmap(QPixmap::fromImage(*fimg));
    QRect fnrec(ml1->geometry().topLeft().x(),ml1->geometry().topLeft().y(),picW,picH*p_hval);
    ml1->setGeometry(fnrec);
    ml1->setPixmap(*fpm);
}


void MainWindow::setP_hval(double v1)
{
    p_hval = v1;
    p_hvalSetted();
}

void MainWindow::btnR()
{
    if(animationState_R == false)
    {
        mAni_rotate->start();
        animationState_R = true;
    }
    else
    {
        animationState_R = false;
        mAni_rotate->pause();
    }

}

void MainWindow::btnS()
{
    if(animationState_S == false)
    {
        mAni_scale->start();
        animationState_S = true;
    }
    else
    {
        animationState_S = false;
        mAni_scale->pause();
    }
}

void MainWindow::btnO()
{
    ml1->setGraphicsEffect(mlGraEff);

    if(animationState_O == false)
    {
        mAni_opacity->start();
        animationState_O = true;
    }
    else
    {
        animationState_O = false;
        mAni_opacity->pause();
    }
}

void MainWindow::btnLM()
{
    if(animationState_LabelMove == false)
    {
        mAni_labelPos->start();
        animationState_LabelMove = true;
    }
    else
    {
        animationState_LabelMove = false;
        mAni_labelPos->stop();
    }
}

void MainWindow::btnLGM()
{
    if(animationState_ImgCopy == false)
    {
        mAni_imgCopy->start();
        animationState_ImgCopy = true;
    }
    else
    {
        animationState_ImgCopy = false;
        mAni_imgCopy->pause();
    }
}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    double x = event->position().toPoint().x();
    double y = event->position().toPoint().y();

    msclpt->setX(x - ml1->geometry().width()/2);
    msclpt->setY(y - ml1->geometry().height()/2);
    QRect mr(*msclpt,QSize(picH,picW));

    ml1->setGeometry(mr);

    int mlx = ml1->geometry().topLeft().x();
    int mly = ml1->geometry().topLeft().y();
    iniAnimationOp(mAni_labelPos,ml1,"pos",10000,QPoint(mlx,mly),QPoint(mlx+200,mly));

}

void MainWindow::iniAnimationOp(QPropertyAnimation *ani,QWidget *obj,QString f_pro,int f_duration,double f_stVal,double f_edVal)
{
    ani->setTargetObject(obj);
    QByteArray pn = f_pro.toUtf8();
    ani->setPropertyName(pn);
    ani->setEasingCurve(QEasingCurve::Linear);
    ani->setLoopCount(-1);
    ani->setDuration(f_duration);
    ani->setStartValue(f_stVal);
    ani->setEndValue(f_edVal);
}

void MainWindow::iniAnimationOp(QPropertyAnimation *ani,QGraphicsOpacityEffect  *gre,QString f_pro,int f_duration,double f_stVal,double f_edVal)
{
    ani->setTargetObject(gre);
    QByteArray pn = f_pro.toUtf8();
    ani->setPropertyName(pn);
    ani->setEasingCurve(QEasingCurve::OutExpo);
    ani->setLoopCount(-1);
    ani->setDuration(f_duration);
    ani->setStartValue(f_stVal);
    ani->setEndValue(f_edVal);
}

void MainWindow::iniAnimationOp(QPropertyAnimation *ani,QWidget *obj,QString f_pro,int f_duration,QPoint sp,QPoint ep)
{
    ani->setTargetObject(obj);
    QByteArray pn = f_pro.toUtf8();
    ani->setPropertyName(pn);
    ani->setEasingCurve(QEasingCurve::Linear);
    ani->setLoopCount(-1);
    ani->setDuration(f_duration);
    ani->setStartValue(sp);
    ani->setEndValue(ep);
}

